package com.example.products.controller;

import com.example.products.pojo.Products;
import com.example.products.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/product")
@RestController
public class ProductsController {
    @Autowired
    private ProductsService productsService;
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @RequestMapping("/save")
    public Map<String, Object> saveProduct(@RequestBody Products product) {
        HashMap<String, Object> map = new HashMap<>();
        int pid = productsService.save(product);
        System.out.println(product.getId());
        if (pid == 1) {
            kafkaTemplate.send("product", product.getId() + "");
            map.put("state", 200);
            map.put("message", "保存成功");
        } else {
            map.put("state", 500);
            map.put("message", "保存失败");
        }
        return map;
    }
    @RequestMapping("/query/{name}")
    public Map<String, Object> queryByName(@PathVariable String name) throws UnsupportedEncodingException {
        Map<String, Object> map = productsService.queryByName(name);
        return map;
    }
}
